package namesrv

import (
	"bbq/common/protocol/remoteCommand"
	"bbq/common/protocol/route"
	"bbq/common/remoting"
	"bbq/namesrv/namesrvLog"
	"bbq/namesrv/processor"
	"bbq/namesrv/routeinfo"
	"github.com/panjf2000/gnet/pkg/pool/goroutine"
)

/*
  控制器入口
*/

type NameSrvController struct {
	Server           *remoting.GnetRemoteServer  //服务器通信
	RouteInfoManager *routeinfo.RouteInfoManager //路由信息
}

//创建
func NewNameSrvController() *NameSrvController {
	server := &NameSrvController{
		Server: remoting.NewGnetRemoteServer(namesrvLog.Log),
		RouteInfoManager: &routeinfo.RouteInfoManager{
			TopicQueueTable:  make(map[string] /*topic*/ []*route.QueueData, 8),
			BrokerAddrTable:  make(map[string] /*brokerName*/ *route.BrokerData, 8),
			ClusterAddrTable: make(map[string] /*topic*/ map[string]int8, 8),
			BrokerLiveTable:  make(map[string] /*topic*/ *routeinfo.BrokerLiveInfo, 8),
		},
	}
	return server
}

//启动
func (c *NameSrvController) Start() {
	c.Server.Config = &remoting.RemoteServerConfig{
		Port: "10000",
	}
	ps := &processor.NamesrvProcessor{
		RouteInfoManager: c.RouteInfoManager,
	}

	c.Server.RegisterProcessor(remoteCommand.REGISTER_BROKER, ps, goroutine.Default())
	//启动
	c.Server.Start()
}

//关机
func (*NameSrvController) Shutdown() {

}
